<?php

namespace app\admin\controller;

use think\Db;
use think\Request;
use think\Validate;
use app\facade\Redis;

use app\models\{Config as Configs, Banner, Message, Annou, User as Users, Kefu, Price, Quotes, Wallet};

class System extends Base
{
    public function index()
    {
        $ModelFun = new Configs();
        $data = $this->request->get();
        $list = $ModelFun->paginate(20, false, ['query' => $this->request->param()])->each(function ($item) {
            if ($item['key_value'] == 'hallconfig' || $item['key_value'] == 'withdrawconfig') {
                $item['value'] = json_decode($item['value'], true);
            }
            if ($item['key_value'] == 'exchange') {
                $price = (new Price())->where('date', date('Y-m-d'))->value('price') ?? 0;
                $item['value'] = $item['value'] * $price;
            }
            return $item;
        });

        return view('', ['list' => $list]);
    }

    public function config_edit()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = $this->request->post();
            $ModelFun = new Configs();
            $list = $ModelFun->where('id', $data['id'])->find();
            if ($list['key_value'] == 'hallconfig' || $list['key_value'] == 'withdrawconfig') {
                $data['value'] = json_encode($data['value'], true);
            }
            if ($list['key_value'] == 'exchange') {
                $price = (new Price())->where('date', date('Y-m-d'))->value('price') ?? 0;
                if (empty($price)) {
                    return json(['status' => 1, 'data' => [], 'msg' => '请先设置代币价格']);
                }
                $list['value'] = round($list['value'] / $price, 2);
            }
            if (isset($data['id']) && $data['id'] > 0) {
                $res = $ModelFun->allowField(true)->save($data, ['id' => $data['id']]);
            } else {
                $res = $ModelFun->allowField(true)->save($data);
            }
            if ($res) {
                return json(['status' => 0, 'data' => [], 'msg' => '更改成功']);
            }
            return json(['status' => 1, 'data' => [], 'msg' => '更改失败']);
        } else {
            $ModelFun = new Configs();
            $id = $this->request->get('id/d', 0);
            $list = $ModelFun->where('id', $id)->find();
            if (!empty($list)) {
                $list = $list->toArray();
            }
            if ($list['key_value'] == 'hallconfig' || $list['key_value'] == 'withdrawconfig') {
                $list['value'] = json_decode($list['value'], true);
            }
            if ($list['key_value'] == 'exchange') {
                $price = (new Price())->where('date', date('Y-m-d'))->value('price') ?? 0;
                $list['value'] = $list['value'] * $price;
            }
            return view('', ['row' => $list]);
        }
    }

    public function ad()
    {
        $ModelFun = new Banner();
        $data = $this->request->get();
        $list = $ModelFun->order('id desc')->paginate(20, false, ['query' => $this->request->param()]);
        return view('', ['list' => $list]);
    }

    //添加修改
    public function ad_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            if (empty($data['id'])) {
                $result = (new Banner())->allowField(true)->save($data);
            } else {
                $result = (new Banner())->allowField(true)->save($data, ['id' => $data['id']]);
            }
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id');
            $row = (new Banner())->where('id', $id)->find();
            return view('', ['row' => $row]);
        }
    }

    public function ad_del()
    {
        $id = trim(input('id'), ',');
        $result = (new Banner())->where('id', $id)->delete();
        if ($result) {
            return json(['status' => 0, 'msg' => '操作成功']);
        } else {
            return json(['status' => 1, 'msg' => '删除失败']);
        }
    }

    public function message()
    {
        $ModelFun = new Message();
        $data = $this->request->get();
        $list = $ModelFun->order('id desc');
        $list = $list->paginate(20, false, ['query' => $this->request->param()])->each(function ($list) {
            $list['images'] = json_decode($list['images'], true) ?? [];
            $list['mobile'] = (new Users())->where('id', $list['user_id'])->value('mobile');
        });
        return view('', ['list' => $list]);
    }

    public function message_add()
    {
        $rdslist = Redis::get('clickreturn');
        if ($rdslist) {
            return json(['code' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
        }
        Redis::setex('clickreturn', 2, 1);
        $ModelFun = new Message();
        $data = $this->request->post();
        if (empty($data['id']) || $data['id'] == 0) {
            return json(['status' => 1, 'msg' => 'ID不能为空']);
        }
        if (empty($data['reply'])) {
            return json(['status' => 1, 'msg' => '回复内容不能为空']);
        }
        $list = $ModelFun->where('id', $data['id'])->find();
        if (empty($list)) {
            return json(['status' => 1, 'msg' => '反馈信息不存在']);
        }
        $res = $ModelFun->where('id', $data['id'])->update(['status' => 2, 'reply' => $data['reply']]);
        if ($res) {
            return json(['status' => 0, 'msg' => '操作成功']);
        } else {
            return json(['status' => 1, 'msg' => '操作失败']);
        }
    }

    public function annou()
    {
        $ModelFun = new Annou();
        $data = $this->request->get();
        $list = $ModelFun->order('id desc');
        if (!empty($data['type'])) {
            $list = $list->where('type', $data['type']);
        }
        if (!empty($data['title'])) {
            $list = $list->where('title', 'like', '%' . $data['title'] . '%');
        }
        $list = $list->paginate(20, false, ['query' => $this->request->param()]);
        return view('', ['list' => $list]);
    }

    //添加修改
    public function annou_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            if (empty($data['id'])) {
                if (!empty($data['type']) && $data['type'] == 3) {
                    $annou = (new Annou())->where(['type' => 3])->find();
                    if (!empty($annou)) {
                        return json(['status' => 1, 'msg' => '帮助信息仅可发布一条,您已发布']);
                    }
                }
                $result = (new Annou())->allowField(true)->save($data);
            } else {
                if (!empty($data['type']) && $data['type'] == 3) {
                    $annou = (new Annou())->where(['type' => 3])->find();
                    if (!empty($annou) && $annou['id'] != $data['id']) {
                        return json(['status' => 1, 'msg' => '帮助信息仅可发布一条,您已发布']);
                    }
                }
                $result = (new Annou())->allowField(true)->save($data, ['id' => $data['id']]);
            }
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id');
            $row = (new Annou())->where('id', $id)->find();
            return view('', ['row' => $row]);
        }
    }

    public function annou_del()
    {
        $id = trim(input('id'), ',');
        $result = (new Annou())->where('id', $id)->delete();
        if ($result) {
            return json(['status' => 0, 'msg' => '操作成功']);
        } else {
            return json(['status' => 1, 'msg' => '删除失败']);
        }
    }

    public function kefu()
    {
        $ModelFun = new Kefu();
        $data = $this->request->get();
        $list = $ModelFun->order('id desc');
        if (!empty($data['type'])) {
            $list = $list->where('type', $data['type']);
        }
        if (!empty($data['number'])) {
            $list = $list->where('number', 'like', '%' . $data['title'] . '%');
        }
        $list = $list->paginate(20, false, ['query' => $this->request->param()]);
        return view('', ['list' => $list]);
    }

    //添加修改
    public function kefu_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            if (empty($data['id'])) {
                $result = (new Kefu())->allowField(true)->save($data);
            } else {
                $result = (new Kefu())->allowField(true)->save($data, ['id' => $data['id']]);
            }
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id');
            $row = (new Kefu())->where('id', $id)->find();
            return view('', ['row' => $row]);
        }
    }

    public function kefu_del()
    {
        $id = trim(input('id'), ',');
        $result = (new Kefu())->where('id', $id)->delete();
        if ($result) {
            return json(['status' => 0, 'msg' => '操作成功']);
        } else {
            return json(['status' => 1, 'msg' => '删除失败']);
        }
    }

    public function price()
    {
        $ModelFun = new Price();
        $data = $this->request->get();
        $list = $ModelFun->order('date desc');
        $list = $list->paginate(20, false, ['query' => $this->request->param()]);
        return view('', ['list' => $list]);
    }

    //添加修改
    public function price_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            //查找日期是否已经存在
            $price = (new Price())->where('date', $data['date'])->find();
            if (empty($data['id'])) {
                if (!empty($price)) {
                    return json(['status' => 1, 'data' => [], 'msg' => '日期价格已添加']);
                }
                $result = (new Price())->allowField(true)->save($data);
            } else {
                if (!empty($price) && $data['id'] != $price['id']) {
                    return json(['status' => 1, 'data' => [], 'msg' => '日期价格已添加']);
                }
                $result = (new Price())->allowField(true)->save($data, ['id' => $data['id']]);
            }
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id');
            $row = (new Price())->where('id', $id)->find();
            return view('', ['row' => $row]);
        }
    }

    public function price_del()
    {
        $id = trim(input('id'), ',');
        $result = (new Price())->where('id', $id)->delete();
        if ($result) {
            return json(['status' => 0, 'msg' => '操作成功']);
        } else {
            return json(['status' => 1, 'msg' => '删除失败']);
        }
    }

    public function quotes()
    {
        $ModelFun = new Quotes();
        $data = $this->request->get();
        $list = $ModelFun->order('id desc');
        $list = $list->paginate(20, false, ['query' => $this->request->param()]);
        return view('', ['list' => $list]);
    }

    //添加修改
    public function quotes_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            if ($data['type'] == 2 && empty($data['coin2'])) {
                return json(['status' => 1, 'msg' => '涨跌幅绑定币种不能为空']);
            }
            //查找日期是否已经存在
            if (empty($data['id'])) {
                $result = (new Quotes())->allowField(true)->save($data);
            } else {
                $result = (new Quotes())->allowField(true)->save($data, ['id' => $data['id']]);
            }
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id');
            $row = (new Quotes())->where('id', $id)->find();
            return view('', ['row' => $row]);
        }
    }

    public function quotes_del()
    {
        $id = trim(input('id'), ',');
        $result = (new Quotes())->where('id', $id)->delete();
        if ($result) {
            return json(['status' => 0, 'msg' => '操作成功']);
        } else {
            return json(['status' => 1, 'msg' => '删除失败']);
        }
    }

    public function wallet()
    {
        $model = new Wallet();
        $data = $this->request->get();
        $list = $model->order('money desc')->paginate(20, false, ['query' => $this->request->param()]);
        foreach ($list as $lists) {
            $lists['private'] = substr($lists['private'], 5);

            $lists['coin_text'] = config()['config']['withdrawtype'][$lists['type']] ?? "";
        }
        return view('', ['list' => $list]);
    }

    public function walletstr()
    {
        $id = $this->request->post('id/d', 0);
        $wallet = (new Wallet())->where('id', $id)->find();
        if (empty($wallet)) {
            return json(['status' => 1, 'msg' => '地址不存在，更新失败']);
        }
        $contact = '0xdac17f958d2ee523a2206206994597c13d831ec7';
        $coinlength = 6;
        $ethkeys = '9D8KX5N8TR55M2F86IR4EEKCKVZH21FY95';
        $url = 'http://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress=' . $contact . '&address=' . $wallet['address'] . '&apikey=' . $ethkeys;
        $resulturl = api_curl($url, []);
        $result = json_decode($resulturl, true);
        if (!empty($result) && $result['status'] == 1 && $result['result'] >= 0) {
            $havmoney = floor($result['result'] * 10000 / pow(10, $coinlength)) / 10000;
            $res = (new Wallet())->where(['id' => $wallet['id']])->update(['money' => $havmoney]);
            if ($res) {
                return json(['status' => 0, 'msg' => '更新成功']);
            }
        }
        return json(['status' => 0, 'msg' => '更新失败']);
    }
}
